// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1
import com.nokia.symbian 1.1
import com.nokia.extras 1.1

Page {
    id: root
    tools: ToolBarLayout {
        ToolButton {
            iconSource: "toolbar-back"
            onClicked: pageStack.pop();
        }
    }

    Flickable {
        id: flick
        anchors { left: parent.left; top: parent.top; right: parent.right; }
        height: (inputContext.visible ? parent.height - inputContext.height : parent.height)
        contentWidth: root.width; contentHeight: colTexts.height + info.height + 10;
        interactive: inputContext.visible && height < contentHeight

        InfoAnimadoBackground {
            id: info
            textoInfo: qsTr("_novo_header")
            textoDescricao: qsTr("_novo_header_desc")
        }

        Column {
            id: colTexts
            anchors { top: info.bottom; horizontalCenter: parent.horizontalCenter }
            spacing: 4

            TextField {
                id: txtRastreio
                width: root.width * 0.85
                maximumLength: 30
                onTextChanged: {
                    txtRastreio.text = txtRastreio.text.toUpperCase();
                }
                placeholderText: qsTr("_rastreio")
            }

            TextField {
                id: txtDescricao
                width: root.width * 0.85
                maximumLength: 50
                placeholderText: qsTr("_lembrete")
            }

            Button {
                text: "Adicionar"
                onClicked: {
                    if (!bancoCare.TrackExiste(txtRastreio.text))
                        internet.Consultar(txtRastreio.text);
                    else
                        dlgTrackExiste.open();
                }
                x: colTexts.x
            }
        }
    }

    Connections {
        target: internet

        onPesquisaIniciada: {
            dlgProgresso.open();
        }

        onPesquisaConcluida: {
            dlgProgresso.close();
        }

        onConsultaCancelada: {
            dlgProgresso.close();
        }

        onSemResposta: {
            dlgSemResposta.open();
        }
    }

    Connections {
        target: bancoCare

        onTrackInvalido: {
            dlgErroTrack.open()
        }

        onFimProcessamento: {
            trackModel.AtualizarTrack(idTrack, txtDescricao.text);
            dlgTrackOk.open();
        }
    }

    DlgProgresso {
        id: dlgProgresso
        onCancelar: internet.CancelarConsulta();
    }

    QueryDialog {
        id:  dlgSemResposta
        titleText: qsTr("_erro")
        icon: "qrc:///fail"
        message: qsTr("_msgErro_01")
        acceptButtonText: qsTr("_ok")
        onAccepted: dlgSemResposta.close();
    }

    QueryDialog {
        id: dlgTrackExiste
        titleText: qsTr("_rastreio")
        icon: "qrc:///info"
        message: qsTr("_msgErro_02")
        acceptButtonText: qsTr("_ok")
        onAccepted: {
            dlgTrackExiste.close();
        }
    }

    QueryDialog {
        id: dlgErroTrack
        titleText: qsTr("_rastreio")
        icon: "qrc:///fail"
        message: qsTr("_msgErro_03")
        acceptButtonText: qsTr("_ok")
        onAccepted: {
            dlgErroTrack.close();
        }
    }

    Dialog {
        id: dlgTrackOk
        title: HeaderDialogs {
            texto: qsTr("_rastreio")
        }
        content: Item {
            anchors.centerIn: parent
            width: childrenRect.width; height: childrenRect.height

            Text {
                text: qsTr("_msgInfo_01")
                color: platformStyle.colorNormalLight
            }
        }
        buttons: ButtonRow {
            width: parent.width;
            spacing: 3
            Button {
                text: qsTr("_visualizar")
                onClicked: {                    
                    bancoCare.CarregarDetalheTrack(txtRastreio.text);
                    pageStack.replace(Qt.resolvedUrl("DetalhePage.qml"));
                    dlgTrackOk.close();
                }
            }
            Button {
                text: qsTr("Novo")
                onClicked: {
                    txtRastreio.text = "";
                    txtDescricao.text = "";
                    dlgTrackOk.close();
                }
            }
            Button {
                text: qsTr("Voltar")
                onClicked: {
                    dlgTrackOk.close();
                    pageStack.pop();
                }
            }
        }
    }
}
